package com.tange.ai.core.sample.binding

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import com.tange.ai.core.sample.utils.Utils
import com.tange.ai.core.sample.databinding.ActivityBindingDirectlyBinding
import com.tange.base.toolkit.Tips
import com.tange.base.ui.design.BasicDialogLoading
import com.tange.feature.binding.query.DeviceBindingDirectly

class BindingDirectlyActivity: AppCompatActivity() {


    private lateinit var binding: ActivityBindingDirectlyBinding

    private var loading: BasicDialogLoading? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityBindingDirectlyBinding.inflate(layoutInflater).apply {
            setContentView(root)
            supportActionBar?.hide()
        }

        Utils.setStatusBar(this, Color.TRANSPARENT)

        binding.next.setOnClickListener { bind() }
    }

    private fun bind() {
        val deviceId = binding.deviceIdEt.text.trim().toString()
        if (TextUtils.isEmpty(deviceId)) {
            Tips.show("empty device id")
            return
        }

        val deviceName = binding.deviceNameEt.text.trim().toString()

        if (loading?.isShowing == true) {
            loading?.dismiss()
        }

        loading = BasicDialogLoading(this).apply {
            setMessage("绑定中...")
        }
        loading?.show()

        DeviceBindingDirectly.require(deviceId, deviceName) {
            loading?.dismiss()
            if (it.success) {
                Tips.show("绑定成功！")
            } else {
                Tips.show("绑定失败: code = ${it.code} , msg = ${it.message}")
            }
        }
    }

}

